package com.xxx.springboot.service.impl;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;
import com.xxx.springboot.handler.UserVerifyHandler;
import com.xxx.springboot.mapper.UserMapper;
import com.xxx.springboot.pojo.entity.User;
import com.xxx.springboot.pojo.vo.UserExportVO;
import com.xxx.springboot.pojo.vo.UserImportVO;
import com.xxx.springboot.service.DemoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * demo service实现类
 */
@Slf4j
@Service
public class DemoServiceImpl implements DemoService {
	
	@Autowired
	private UserMapper userMapper;

	@Autowired
	private JdbcTemplate primaryJdbcTemplate;

	@Autowired
	private JdbcTemplate secondaryJdbcTemplate;

	@Autowired
	private UserVerifyHandler userVerifyHandler;

	/**
	 * 4. 测试JTA多数据源事务
	 */
	@Transactional
	@Override
	public int testJTA() {
		// 更新第1个db
		int m = primaryJdbcTemplate.update("update t_user set user_name = ? where id = ?", "user11_jta", 1);
		log.info("testJTA|m="+m);
		// 更新第2个db
		int n = secondaryJdbcTemplate.update("update t_user set user_name = ? where id = ?", "user22_jta", 2);
		log.info("testJTA|n="+n);
		// 异常情况：抛出异常，验证分布式事务是否能够回滚，注意，如果在JTA环境下禁用JTA事务，则会使用本地事务，但是回滚时只会回滚第1个事务
		throw new RuntimeException("exception");
//		// 正常情况：返回执行成功的记录数
//		return m + n;
	}
	
	@Override
	public User getByName(String username) {
		log.info("getByName|username:{}", username);
		return userMapper.getByName(username);
	}

	/**
	 * 2. easy poi 导入excel
	 * @param file
	 * @throws Exception
	 */
	@Override
	public String importExcelByEasyPoi(MultipartFile file) throws Exception {
		// 设置导入相关参数
		ImportParams importParams = new ImportParams();
		// excel标题行数(默认0)
//		importParams.setTitleRows(1);
//		importParams.setHeadRows(1);
//		importParams.setStartRows(1);
		// 是否需要校验excel
		importParams.setNeedVerify(true);
		// 设置excel校验器
		importParams.setVerifyHandler(userVerifyHandler);

		// 执行导入
		ExcelImportResult<UserImportVO> result = ExcelImportUtil.importExcelMore(file.getInputStream(), UserImportVO.class, importParams);
		if(!result.isVerifyFail() && !CollectionUtils.isEmpty(result.getList())) { // 校验正确 && 结果列表不为空
			List<UserImportVO> voList = result.getList();
			voList.forEach(vo -> {
				log.info("importExcelByEasyPoi_vo:{}", vo.toString());
			});
			return "文档导入成功";
		} else {
			List<UserImportVO> failList = result.getFailList();
			failList.forEach(vo -> {
				log.info("importExcelByEasyPoi_fail:{}", vo.toString());
			});
			return "文档校验失败";
		}
	}

	@Override
	public List<UserExportVO> exportExcelByEasyPoi() {
		List<UserExportVO> voList = new ArrayList<>();
		UserExportVO vo = new UserExportVO();
		vo.setName("张三");
		vo.setSex(1);
		vo.setBirthDay(new Date());
		vo.setPhone("180123456789");
		vo.setEmail("aaaa@test.com");
		voList.add(vo);
		vo = new UserExportVO();
		vo.setName("李四");
		vo.setSex(2);
		vo.setBirthDay(new Date());
		vo.setPhone("181234567890");
		vo.setEmail("bbbb@test.com");
		voList.add(vo);
		return voList;
	}

}
